package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




public class WiliFilterAction implements Filter {
	  
	
	public void destroy(){
		
	}
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request= (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String ur = request.getRequestURI();
		if(ur.contains("/StartPage")||ur.contains("/login.jsp")||ur.contains("/QRlogin.jsp")
				||ur.contains("/GuanLogin")||ur.contains("/jquery")
				||ur.contains("/icon")||ur.contains("/img")||ur.contains("/TransferServlet")
				||ur.contains("/xieyi.html")||ur.contains("/ver")||ur.contains("/log")
				||ur.contains("/mycenter_uploadHead.action")||ur.contains("/mycenter_uploadPictures.action")){
				chain.doFilter(request, response);
		}else if(ur.contains("/Guan")||ur.contains("/jsp")){
			Object name = request.getSession().getAttribute("session");
			if(name==null){
				request.setAttribute("No", "用户未登录");
				String chr = "/";
				int count = 0;
				for (int j = 0; j < ur.length(); j++) {
					if (chr.equals(ur.charAt(j)+"")){				
						count++;}
					}
				String url1 ="";
				if(count==2){
					url1="jsp/QRlogin.jsp";
				}
				if(count==3){
					url1="/jsp/QRlogin.jsp";
				}
				if(count==4){
					url1="../QRlogin.jsp";
				}
				if(count==5){
					url1="../QRlogin.jsp";
				}
				request.getRequestDispatcher(url1).forward(request,response);
			}else{
				chain.doFilter(request, response);
			}
		}else{
			/*PrintWriter out = null;
			JSONObject j =new JSONObject();
			try {
				out =response.getWriter();
				String token =null;
				String id = null;
				token = request.getParameter("token");
				id = request.getParameter("userid");
			 if(token!=null&&!token.equals("")){
				 ServletContext application = request.getSession().getServletContext();
				 String test = application.getAttribute(id).toString();
					if(test.equals(token)){
						chain.doFilter(request, response);
					}else{
						j.put("code",-2);
						out.print(j);
					}
				}else{
					j.put("code",-2);
					out.print(j);
				}
			} catch (Exception e) {
				j.put("code",-1);
				out.print(j);
				e.printStackTrace();
			}*/
			chain.doFilter(request, response);
		}
	}
	
	
	/**
	 * 
	 * 系统初始化
	 */
	public void init(FilterConfig arg0) throws ServletException {
	}
	
	
	
	
	
	
}
